package ru.spb.nicetu.schoolproject.data;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.util.List;

/**
 * Created by dchernyshov on 09.04.15.
 */
public abstract class HibernateTblModel<T> extends AbstractTableModel {
    protected List<T> list;

    protected HibernatePagination<T> hibernatePagination;

    public HibernateTblModel() {}

    public HibernateTblModel(Class<T> clazz, int pageSize) {
        hibernatePagination = new HibernatePagination(clazz, pageSize);
        list = hibernatePagination.getPage(0);
    }

    public void nextPage() {
        if (hibernatePagination.hasNextPage()) {
            list = hibernatePagination.getNextPage();
        }
    }

    public void previousPage() {
        if (hibernatePagination.hasPreviousPage()) {
            list = hibernatePagination.getPreviousPage();
        }
    }

    public void updateCurrentPage() {
        list = hibernatePagination.updatePage();
        fireTableDataChanged();
    }

    @Override
    public int getRowCount() {
        return list.size();
    }

    public boolean removeObject(int index) {
        return hibernatePagination.removeObject(list.get(index));
    }

    public T getObject(int index) {
        return list.get(index);
    }


}
